package com.example.ranrabbitmq.controller;

import com.example.ranrabbitmq.pojo.Rabbitmq;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/***
 * NAME:Ran
 * TIME:2024/12/20
 * rest controller测试类
 */
@RestController
public class RabbitMQController {

    //    注入rabbitmq
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //发送mes消息
    @GetMapping("/send")
    public void send(@RequestParam String mes) {
//   参数：交换机 key 消息
        rabbitTemplate.convertAndSend(Rabbitmq.getExchange(), Rabbitmq.getKey(), mes);
    }

    // 使用@RabbitListener注解接收RanQueue队列消息
    @RabbitListener(queues = "RanQueue")
    public void rabbitMes(String mes) {
        System.out.println("监听队列消息为：" + mes);
    }
}
